package com.example.gymmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Bill;
import com.example.gymmanagement.domain.User;
import com.example.gymmanagement.service.BillService;
import com.example.gymmanagement.mapper.BillMapper;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 13726256113
* @description 针对表【bill】的数据库操作Service实现
* @createDate 2023-06-07 09:42:12
*/
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill>
    implements BillService{
    @Autowired
    private BillMapper billMapper;

    @Override
    public boolean insert(Bill bill) {
        return billMapper.insert(bill)>0;
    }

    @Override
    public boolean delete(Integer id) {
        return this.billMapper.deleteById(id)>0;
    }

    @Override
    public Result selectByCategory(Integer billUser, String billtype, Integer index, Integer size) {
        //默认第一页开始，每页5条
        if(index == null || index==0){
            index=1;
        }
        if(size==null || size==0){
            size=5;
        }
        Page<Bill> pageInfo = new Page(index,size);
        LambdaQueryWrapper<Bill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询类型不为空作为查询条件
        lambdaQueryWrapper.like(null!=billUser,Bill::getBillUser,billUser).like(null!=billtype,Bill::getBillType,billtype);
        Page<Bill> billList = (Page)this.billMapper.selectPage(pageInfo,lambdaQueryWrapper);
        Integer code = billList != null ? StatusCode.GET_OK : StatusCode.GET_ERR;
        String msg = billList != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code, billList, msg);
    }

    @Override
    public boolean updateBill(Bill bill) {
        return this.billMapper.updateById(bill)>0;
    }

    @Override
    public Result selectAllBill(Integer index, Integer size) {
        //默认第一页开始，每页5条
        if(index == null ||index==0){
        index=1;
        }
        if(size==null||size==0){
            size=5;
        }
        Page<Bill> pageInfo = new Page(index,size);
        LambdaQueryWrapper<Bill> lambdaQueryWrapper = new LambdaQueryWrapper();
        Page<Bill> billList = (Page) this.billMapper.selectPage(pageInfo,lambdaQueryWrapper);
        Integer code = billList!=null? StatusCode.GET_OK:StatusCode.GET_ERR;
        String msg = billList!=null?"查询成功":"查询失败";
        return new Result(code,billList,msg);
    }

    @Override
    public Result calculateBills() {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        //查询各个bill_type，price字段总值
        wrapper.select("bill_type","SUM(bill_price) AS total").groupBy("bill_type");
        List<Map<String,Object>> result = billMapper.selectMaps(wrapper);
        //计算总额
        double incomeTotal = 0.0;
        double expensesTotal=0.0;
        Map<String, Double> resultMap = new HashMap<>();
        for (Map<String,Object> row:result){
            //获取账单类型
            String type = (String)row.get("bill_type");
            double total = Double.valueOf(row.get("total").toString());
            resultMap.put(type,total);
            //根据type值计算收入和支出
            if ("器材租借".equals(type)){
                incomeTotal=incomeTotal+ total;
            }if ("器材赔偿".equals(type)){
                incomeTotal=incomeTotal+ total;
            }if ("场地租用".equals(type)){
                incomeTotal=incomeTotal+ total;
                System.out.println(incomeTotal);
            }if ("器材购置".equals(type)){
                expensesTotal=expensesTotal+ total;
            }if ("器材报修".equals(type)){
                expensesTotal=expensesTotal+ total;
            }
        }
        resultMap.put("总收入",incomeTotal);
        resultMap.put("总支出",expensesTotal);
        return new Result(StatusCode.GET_OK,resultMap,"账单统计完成");
    }
}




